package com.buildings.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.buildings.R;
import com.buildings.base.BaseActivity;
import com.buildings.bean.AddressBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.DataUtils;
import com.buildings.util.Utils;
import com.google.gson.Gson;

import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;

public class ResetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText etPassword;
    private EditText etPasswordAgain;
    private EditText etPhone;
    private EditText etCode;
    private Button btnCode;
    private Button btnCommit;
    private TimeCount time;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        time = new TimeCount(60000, 1000);
        initView();
    }

    private void initView(){
        findViewById(R.id.iv_back).setOnClickListener(this);
        etPassword = findViewById(R.id.et_password);
        etPasswordAgain = findViewById(R.id.et_password_again);
        etPhone = findViewById(R.id.et_phone);
        etCode = findViewById(R.id.et_code);

        btnCode = findViewById(R.id.btn_code);
        btnCode.setOnClickListener(this);
        btnCommit = findViewById(R.id.btn_commit);
        btnCommit.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("重置密码");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;

            case R.id.btn_code:
               String phone = etPhone.getText().toString();//手机号
                if (TextUtils.isEmpty(phone.trim())) {
                    lastClickTime = System.currentTimeMillis();
                    CustomToast.showToast(activity, "手机号不能为空");
                    return;
                }
                //多次点击只执行一次
                if (Math.abs(lastClickTime - System.currentTimeMillis()) < 3000) {
                    lastClickTime = 0;
                    return;
                }
                lastClickTime = System.currentTimeMillis();
                time.start();
                break;
            case R.id.btn_commit:
                if(TextUtils.isEmpty(etPhone.getText().toString())){
                    CustomToast.showToast(activity,"请输入手机号");
                    return;
                }
                if(etPhone.getText().toString().length() != 11){
                    CustomToast.showToast(activity,"请输入正确手机号");
                    return;
                }
                if(TextUtils.isEmpty(etPassword.getText().toString())){
                    CustomToast.showToast(activity,"请输入密码");
                    return;
                }
                if(TextUtils.isEmpty(etPasswordAgain.getText().toString())){
                    CustomToast.showToast(activity,"请输入确认密码");
                    return;
                }

                if(TextUtils.isEmpty(etCode.getText().toString())){
                    CustomToast.showToast(activity,"请输入验证码");
                    return;
                }
                //调用重置密码接口
                break;
        }
    }
    private void GetCodeHttp() {
        JsonBuilder jsonBuilder = new JsonBuilder(this);
        jsonBuilder.put("doctorMobile", etPhone.getText().toString());
        jsonBuilder.put("flag", "A");

        MyHttpClient
                .newBuilder()
                .post()
                .url("")
                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack() {
                    @Override
                    public void onSuccess(Object o) {
                        super.onSuccess(o);

                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
//                        if (myDialog != null)
//                            myDialog.dismiss();
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
//                        if (myDialog != null)
//                            myDialog.dismiss();
                    }
                });

    }

    /* 倒计时类 */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {
            btnCode.setText("重新获取");
            btnCode.setClickable(true);

        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            btnCode.setClickable(false);
            btnCode.setText("（" + millisUntilFinished / 1000 + "s）");
        }
    }

}
